/*******************************************************************************
 * Copyright (c) 2008-2010 Cedric Vidal
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Cedric Vidal - original idea, initial contribution and API
 *******************************************************************************/

/**
 * 
 */
package org.trans4j.spring.tests.mappers.xml;

import org.trans4j.CreateRule;
import org.trans4j.spring.tests.mappers.ParentMapping;
import org.trans4j.spring.tests.model.LeftChild;
import org.trans4j.spring.tests.model.LeftParent;
import org.trans4j.spring.tests.model.RightParent;

public class ParentMapper implements ParentMapping {

	private ChildMapper childMapper;

	public ChildMapper getChildMapper() {
		return childMapper;
	}

	public void setChildMapper(ChildMapper childMapper) {
		this.childMapper = childMapper;
	}

	public RightParent parent(LeftParent b) {
		RightParent a = createParent(b.getLeftName());
		for (LeftChild c : b.getLeftChilds()) {
			a.getRightChilds().add(childMapper.child(c));
		}
		a.setChild1(childMapper.child(b.getLeftChilds().get(0)));
		a.setChild2(childMapper.child(b.getLeftChilds().get(1)));
		return a;
	}

	@CreateRule
	protected RightParent createParent(String leftName) {
		RightParent a = new RightParent();
		a.setRightName(leftName.replace("left", "right"));
		return a;
	}

}